/**
 * @author João Luís Palma Cardeira - hippie.stereo@gmail.com
 * @version 1.0
 * @date 06/06/2013
 * @copyright (C) :-)
 * @note Applicação para o trabalho de EDA 2012/2013
 * @see main : RunApp
 */
import com.xeiam.xchart.BitmapEncoder;
import com.xeiam.xchart.Chart;
import com.xeiam.xchart.Series;
import com.xeiam.xchart.SeriesLineStyle;
import com.xeiam.xchart.SeriesMarker;
import com.xeiam.xchart.StyleManager.LegendPosition;
import com.xeiam.xchart.SwingWrapper;
import java.awt.Color;
import java.io.IOException;
 
public class PlotGraph
{

	/** 
	 * nomMethode - XXX
	 * @return [description]
	 * @param [parameter-name] [description]
	 * @param [parameter-name] [description]
	 * @exception   Exception1  description de la première exception 
	 * @see UneAutreClasse#UneAutreMethode 
	 */

	private String imgName = null; 
	private Chart chart = null;
	private double[] xData = null;
	private double[] yDataTeoric = null;
	private double[] yDataXperiment = null;

	/** 
	 * nomMethode - XXX
	 * @return [description]
	 * @param [parameter-name] [description]
	 * @param [parameter-name] [description]
	 * @exception   Exception1  description de la première exception 
	 * @see UneAutreClasse#UneAutreMethode 
	 */
	
	protected PlotGraph(String inNameImg, int inSizeH, int inSizeV, double[] inDataX, double[] inDataTeoric, double[] inDataXperiment)
	{
		this.imgName = inNameImg;
		this.chart = new Chart(inSizeH, inSizeV);
		this.xData = inDataX;
		this.yDataTeoric = inDataTeoric;
		this.yDataXperiment = inDataXperiment;
	}

	/** 
	 * nomMethode - XXX
	 * @return [description]
	 * @param [parameter-name] [description]
	 * @param [parameter-name] [description]
	 * @exception   Exception1  description de la première exception 
	 * @see UneAutreClasse#UneAutreMethode 
	 */
	
	protected Boolean savePng()
	{
		Boolean isGood = false;
		
		try
		{
			BitmapEncoder.savePNG(this.chart, this.imgName + ".png");
			isGood = true;
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		
		return isGood;
	}

	/** 
	 * nomMethode - XXX
	 * @return [description]
	 * @param [parameter-name] [description]
	 * @param [parameter-name] [description]
	 * @exception   Exception1  description de la première exception 
	 * @see UneAutreClasse#UneAutreMethode 
	 */
	
	protected void doPlot(String inTitlePlot, String inTitleX, String inTitleY, String inLabelDataTeoric, String inLabelDataXperiment)
	{
		chart.setChartTitle(inTitlePlot);
		chart.setXAxisTitle(inTitleX);
		chart.setYAxisTitle(inTitleY);
		chart.getStyleManager().setLegendBackgroundColor(Color.PINK);
		chart.getStyleManager().setLegendPosition(LegendPosition.OutsideE);
		 
		Series series = chart.addSeries(inLabelDataTeoric, this.xData, this.yDataTeoric);
		series.setLineStyle(SeriesLineStyle.NONE);
		series.setMarker(SeriesMarker.DIAMOND);
		series.setMarkerColor(Color.BLUE);
		   
		Series series2 = chart.addSeries(inLabelDataXperiment, this.xData, this.yDataXperiment);
		
		series2.setMarker(SeriesMarker.SQUARE);
		series2.setLineStyle(SeriesLineStyle.DASH_DASH);
		series2.setLineColor(Color.RED);
		   
		chart.getStyleManager().setPlotPadding(40);
		
		if(this.findMin(this.yDataXperiment) < this.findMin(this.yDataTeoric))
		{
			chart.getStyleManager().setYAxisMin(this.findMin(this.yDataXperiment));
		}
		else
		{
			chart.getStyleManager().setYAxisMin(this.findMin(this.yDataTeoric));	
		}

		if(this.findMax(this.yDataXperiment) > this.findMax(this.yDataTeoric))
		{
			chart.getStyleManager().setYAxisMax(this.findMax(this.yDataXperiment));
		}
		else
		{
			chart.getStyleManager().setYAxisMax(this.findMax(this.yDataTeoric));
		}		
		chart.getStyleManager().setXAxisMin(this.xData[this.xData.length - this.xData.length]);
		chart.getStyleManager().setXAxisMax(this.xData[this.xData.length - 1]);
		  
		chart.getStyleManager().setLegendBackgroundColor(Color.PINK);
		new SwingWrapper(chart).displayChart();
	}

	/** 
	 * nomMethode - XXX
	 * @return [description]
	 * @param [parameter-name] [description]
	 * @param [parameter-name] [description]
	 * @exception   Exception1  description de la première exception 
	 * @see UneAutreClasse#UneAutreMethode 
	 */
	
	private double findMax(double[] inData)
	{
		double dblMax = 0;
		
		for (int i = 1; i < inData.length; i++)
		{
			if(inData[i - 1] < inData[i])
			{
				dblMax = inData[i];
			}
		}

		return dblMax;
	}

	/** 
	 * nomMethode - XXX
	 * @return [description]
	 * @param [parameter-name] [description]
	 * @param [parameter-name] [description]
	 * @exception   Exception1  description de la première exception 
	 * @see UneAutreClasse#UneAutreMethode 
	 */
	
	private double findMin(double[] inData)
	{
		double dblMin = 0;
		
		for (int i = 1; i < inData.length; i++)
		{
			if(inData[i - 1] > inData[i])
			{
				dblMin = inData[i];
			}
		}

		return dblMin;
	}	
}